package com.ruoyi.system.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QShouye;
import com.ruoyi.system.service.IQShouyeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页用值Controller
 * 
 * @author ruoyi
 * @date 2024-06-24
 */
@Controller
@RequestMapping("/system/shouye")
public class QShouyeController extends BaseController
{
    private String prefix = "system/shouye";

    @Autowired
    private IQShouyeService qShouyeService;

    @RequiresPermissions("system:shouye:view")
    @GetMapping()
    public String shouye()
    {
        return prefix + "/shouye";
    }

    /**
     * 查询首页用值列表
     */
    //@RequiresPermissions("system:shouye:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QShouye qShouye)
    {
        startPage();
        QShouye res = new QShouye();
        res.setqAlert("10");

        res.setqImjtlv("100%");
        res.setqImtelzxs("2112");



        int  telTotal = qShouyeService.selectJqznwhs();

        int  telwjtTotal = qShouyeService.selectJqznwhswjt();


        // 计算百分比
        double percentage = (double) (telTotal-telwjtTotal) / telTotal * 100;

        // 格式化输出百分比，保留两位小数
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        String formattedPercentage = decimalFormat.format(percentage);

        System.out.println("百分比是：" + formattedPercentage + "%");

        res.setqRgjtlv(telTotal + "");


        //IM聊天数
        res.setqJqznwhs(String.valueOf(qShouyeService.selectJqznwhs()));
        res.setqImwhzs(String.valueOf(qShouyeService.selectqImwhzs()));

        float num = qShouyeService.selectqImavethsc();
        DecimalFormat df = new DecimalFormat("#.00");
        String formatted = df.format(num);
        System.out.println(formatted); // 输出：123.46
        res.setqImavethsc(String.valueOf(formatted)+'秒');

        //外呼电话数
        res.setqRgwhzs((qShouyeService.selectqRgwhzs())+"");
         num = qShouyeService.selectqRgavethsc();
         df = new DecimalFormat("#.00");
         formatted = df.format(num);
        System.out.println(formatted); // 输出：123.46
        res.setqRgavethsc(String.valueOf(formatted)+'秒');




        List<QShouye> list = new ArrayList<QShouye>();
        list.add(res);
        return getDataTable(list);
    }

    /**
     * 导出首页用值列表
     */
    //@RequiresPermissions("system:shouye:export")
    @Log(title = "首页用值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QShouye qShouye)
    {
        List<QShouye> list = qShouyeService.selectQShouyeList(qShouye);
        ExcelUtil<QShouye> util = new ExcelUtil<QShouye>(QShouye.class);
        return util.exportExcel(list, "首页用值数据");
    }

    /**
     * 新增首页用值
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存首页用值
     */
   // @RequiresPermissions("system:shouye:add")
    @Log(title = "首页用值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QShouye qShouye)
    {
        return toAjax(qShouyeService.insertQShouye(qShouye));
    }

    /**
     * 修改首页用值
     */
   // @RequiresPermissions("system:shouye:edit")
    @GetMapping("/edit/{qSid}")
    public String edit(@PathVariable("qSid") Long qSid, ModelMap mmap)
    {
        QShouye qShouye = qShouyeService.selectQShouyeByQSid(qSid);
        mmap.put("qShouye", qShouye);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页用值
     */
   // @RequiresPermissions("system:shouye:edit")
    @Log(title = "首页用值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QShouye qShouye)
    {
        return toAjax(qShouyeService.updateQShouye(qShouye));
    }

    /**
     * 删除首页用值
     */
   // @RequiresPermissions("system:shouye:remove")
    @Log(title = "首页用值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qShouyeService.deleteQShouyeByQSids(ids));
    }
}
